package com.example;

import javafx.fxml.FXML;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.control.PasswordField;
import javafx.scene.control.Alert;
import javafx.scene.paint.Color;

import java.sql.SQLException;

// 重置用户密码界面控制器
public class AdminResetPasswordController {

    @FXML private TextField usernameField;
    @FXML private PasswordField newPasswordField;
    @FXML private Label messageLabel;

    private UserDAO userDAO = new UserDAO();

    @FXML
    private void handleResetPassword() {
        String username = usernameField.getText().trim();
        String newPassword = newPasswordField.getText().trim();

        if (username.isEmpty() || newPassword.isEmpty()) {
            messageLabel.setTextFill(Color.RED);
            messageLabel.setText("用户名和新密码都不能为空！");
            return;
        }

        try {
            // TODO: 在实际应用中，应该先验证用户名是否存在
            userDAO.updatePassword(username, newPassword);
            messageLabel.setTextFill(Color.GREEN);
            messageLabel.setText("用户 " + username + " 的密码已成功重置！");
            // 清空输入框
            usernameField.clear();
            newPasswordField.clear();
        } catch (SQLException e) {
            e.printStackTrace();
            messageLabel.setTextFill(Color.RED);
            messageLabel.setText("重置密码失败：" + e.getMessage());
        } catch (Exception e) {
             e.printStackTrace();
             messageLabel.setTextFill(Color.RED);
             messageLabel.setText("发生未知错误：" + e.getMessage());
        }
    }

} 